package com.roadjava.rbac.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.ClientConfig;
import com.roadjava.rbac.service.CosService;
import com.roadjava.rbac.service.impl.CosServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;

@Configuration
public class CosConfig {

    @Value("${oss.endpoint}")
    private String endpoint;

    @Value("${oss.secret-id}")
    private String secretId;

    @Value("${oss.secret-key}")
    private String secretKey;

    @Value("${oss.bucket-name}")
    private String bucketName;

    @Bean
    @Primary
    public COSClient cosClient() {
        BasicCOSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region(endpoint);
        ClientConfig clientConfig = new ClientConfig(region);
        return new COSClient(cred, clientConfig);
    }

    @Bean
    public CosService objectStorageService(COSClient cosClient) {
        return new CosServiceImpl(cosClient);
    }
}